package com.cashoutgaming.client.modules.shell;

import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewWithUiHandlers;
import com.sencha.gxt.core.client.XTemplates;
import com.sencha.gxt.widget.core.client.container.BorderLayoutContainer;
import com.sencha.gxt.widget.core.client.container.BorderLayoutContainer.BorderLayoutData;
import com.sencha.gxt.widget.core.client.container.CenterLayoutContainer;
import com.sencha.gxt.widget.core.client.container.SimpleContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;

/**
 * This is the admin tool desktop view. There are presently no requirements to
 * have any other form factor views for the admin tool, so this will be the only
 * one
 */
public class DesktopShellView extends ViewWithUiHandlers<DesktopShellUiHandlers> implements DesktopShellPresenter.MyView {

	private BorderLayoutContainer blContainer;

	private DesktopShellStyle style;

	interface DesktopShellStyle extends CssResource {

		String container();

		String window();

		String footer();
	}

	interface StyleResources extends ClientBundle {

		@Source("DesktopShellCss.css")
		DesktopShellStyle css();
	}

	interface DesktopShellCssTemplate extends XTemplates {

		@XTemplate(source = "DesktopShellCssTemplate.txt")
		SafeHtml generateCustomCss(String mainBgColor, String mainBgImage);
	}

	@Inject
	public DesktopShellView() {
		blContainer = new BorderLayoutContainer();
		final StyleResources resources = GWT.create(StyleResources.class);
		resources.css().ensureInjected();
		style = resources.css();

		blContainer.setStylePrimaryName(style.container());

		// String logo =
		// IconHelper.getHTML(CommonToolImages.INSTANCE.getPsLogo());TODO fix
		// icons
		// String logo = "";
		// HTML poweredBy = new HTML(logo);
		// // poweredBy.setStyleName(style.footerMargin());
		// CenterLayoutContainer footer = new CenterLayoutContainer();
		// poweredBy.setStylePrimaryName(style.footer());
		// footer.add(poweredBy);
		//
		// BorderLayoutData southData = new BorderLayoutData();
		// southData.setCollapsible(false);
		// southData.setSplit(false);
		// southData.setSize(35);
		// blContainer.setSouthWidget(footer, southData);

		this.initWidget(blContainer);
	}

	@Override
	public void setInSlot(Object slot, IsWidget content) {
		if (slot == DesktopShellPresenter.TYPE_MAINPANEL) {
			SimpleContainer simple = new SimpleContainer();
			simple.setBorders(false);
			simple.setWidget(content);
			simple.getWidget().setStylePrimaryName(style.window());
			blContainer.setCenterWidget(simple);
			getUiHandlers().mainSlotChanged(content);
		}
		else if (slot == DesktopShellPresenter.TYPE_TOOLBAR) {
			buildApplicationToolbar(content);
		}
		blContainer.forceLayout();
	}

	public void buildApplicationToolbar(IsWidget tb) {
		final VerticalLayoutContainer vlc = new VerticalLayoutContainer();
		vlc.setBorders(false);
		vlc.add(tb, new VerticalLayoutData(1, 1));

		BorderLayoutData northData = new BorderLayoutData();
		northData.setCollapsible(false);
		northData.setSplit(false);
		northData.setSize(50);
		blContainer.setNorthWidget(vlc, northData);
	}
}
